package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bringway.framework.util.DateUtils;
import com.bringway.p2p.dao.JkglDao;
import com.bringway.p2p.service.JkglService;
import com.bringway.p2p.util.NumberUtil;

import net.sf.json.JSONObject;

/**
 * @author js
 * @version 创建时间：2016年6月20日 
 * @功能：借款人管理界面
 */
@Service
public class JkglServiceImpl implements JkglService{
	
	Logger logger = Logger.getLogger(JkglServiceImpl.class);
	@Autowired
	private JkglDao jkglDao;

	@Override
	public List queryJkglHkList(JSONObject params) {
		return jkglDao.queryJkglHkList(params);
	}

	@Override
	public int getJkglHkCount(JSONObject params) {
		return jkglDao.getJkglHkCount(params);
	}

	@Transactional("p2p")
	@Override
	public boolean saveJkglHk(JSONObject params) {
		boolean isSaveJkglHkSuc = false;
		String hkXxId = null;
		boolean issaveJkrHkxxSuc=false;
		boolean isupdateZqzrlbZt=false;
		boolean isaddZcDetailsSuc=false;
		isSaveJkglHkSuc=jkglDao.saveJkglHk(params);//先更新用户账户余额和可用金额
		if (isSaveJkglHkSuc) {
			params.put("hkstate", "01");
			hkXxId=jkglDao.saveJkrHkxx(params);//还款成功,保存还款信息到借款人还款表
			if (StringUtils.isNotEmpty(hkXxId)) {
				issaveJkrHkxxSuc=true;
				params.put("hkXxId", hkXxId);
			}
			//更新t_zhfw_zqzrlb表中项目状态ZT,由前台更新
			/*if ("02".equals(params.optString("hkzl"))) {
				isupdateZqzrlbZt=true;//归还收益状态不变
			}else if ("01".equals(params.optString("hkzl"))) {
				params.put("xmzt", 4);
				isupdateZqzrlbZt=jkglDao.updateZqzrlbZt(params);
			}*/
			// 关联到资产详细表
			isaddZcDetailsSuc=jkglDao.addZcDetails(params);
		}else {
			params.put("hkstate", "02");
			hkXxId=jkglDao.saveJkrHkxx(params);//还款失败,保存还款信息到借款人还款表
		}
		if(isSaveJkglHkSuc && isaddZcDetailsSuc && issaveJkrHkxxSuc){
			logger.info("借款人还款时关联分档信息成功！ ");
			return true;
		}else{
			logger.info("借款人还款失败！ ");
			return false;
		}
	}

	@Override
	public List queryHkmxList(JSONObject params) {
		// TODO Auto-generated method stub
		return jkglDao.queryHkmxList(params);
	}

	@Override
	public int getHkmxCount(JSONObject params) {
		// TODO Auto-generated method stub
		return jkglDao.getHkmxCount(params);
	}

	@Override
	public Map<String, Object> isValidJkglHK(String gyh,String id) {
		// TODO Auto-generated method stub
		return jkglDao.isValidJkglHK(gyh,id);
	}

	@Override
	public boolean isHkBj(JSONObject params) {
		// TODO Auto-generated method stub
		return jkglDao.isHkBj(params);
	}

	@Override
	public List<Map<String, Object>> listYhkmx(JSONObject params) {
		//更新募集完成的项目应还款信息到应还款表--t_zhfw_jkr_yhk
		this.jkglDao.updateYhkmx();
		return jkglDao.listYhkmx(params);
	}

	@Override
	public int getYhkmxCount(JSONObject params) {
		return jkglDao.getYhkmxCount(params);
	}

	@Override
	public List<Map<String, Object>> listMarryHk(JSONObject params) {
		return jkglDao.listMarryHk(params);
	}

	@Override
	public int getMarryHkCount(JSONObject params) {
		return jkglDao.getMarryHkCount(params);
	}

	@Override
	public List<Map<String, Object>> listMarryYhk(JSONObject params) {
		return jkglDao.listMarryYhk(params);
	}

	@Override
	public List<Map<String, Object>> listMarryHedHk(JSONObject params) {
		return jkglDao.listMarryHedHk(params);
	}

	@Override
	@Transactional("p2p")
	public JSONObject saveMarryHk(String id,String hkId) {
		JSONObject result = new JSONObject();
		if (StringUtils.isEmpty(id)||StringUtils.isEmpty(hkId)) {
			result.put("res", "0");
			result.put("msg", "参数有误,请重新操作!");
			return result;
		}
		//应还款信息
		Map<String, Object> yhkInfo=jkglDao.getYhkInfoById(id);
		//已还款日期
		String hkDate=jkglDao.getYqInfo(hkId);
		//应还款日期
		String yhkDate=MapUtils.getString(yhkInfo, "yhkDate");
		Map<String, Object> yhkParam=new HashMap<>();
		yhkParam.put("id", id);
		yhkParam.put("hkDate", hkDate);
		yhkParam.put("hkId", hkId);
		int yqts=jkglDao.getCompareDays(hkDate, yhkDate);
		yhkParam.put("yqts", yqts);
		yhkParam.put("yqje",0);
		if (yqts>0) {
			yhkParam.put("state", "2002");
		}else {
			yhkParam.put("state", "2001");
		}
		//更新应还款记录
		int updateYhkInfo=jkglDao.updateYhkInfo(yhkParam);
		if (updateYhkInfo<=0) {
			throw new RuntimeException();//手动抛异常
		}
		//更新已还款记录
		String hkids[]=StringUtils.split(hkId, ",");
		for (String string : hkids) {
			yhkInfo.put("id", string);
			int updateHedHkInfo=jkglDao.updateHedHkInfo(yhkInfo);
			if (updateHedHkInfo<=0) {
				throw new RuntimeException();//手动抛异常
			}
		}
		//判断是否还款完成,完成则修改标的状态4
		JSONObject paramsYhk=new JSONObject();
		paramsYhk.put("xmid", MapUtils.getString(yhkInfo, "REF_XM_ID"));
		paramsYhk.put("username", MapUtils.getString(yhkInfo, "REF_JKR_USERNAME"));
		List<Map<String, Object>> listYhk=jkglDao.listMarryYhk(paramsYhk);
		if (listYhk.size()<=0) {
			//更新项目状态
			int count=jkglDao.updateXmzt(MapUtils.getString(yhkInfo, "REF_XM_ID"));
			if (count<=0) {
				throw new RuntimeException("更新项目状态失败");//手动抛异常
			}
		}
		result.put("res", "1");
		result.put("msg", "匹配成功!");
		return result;
	}
	public JSONObject marryOldHkByUser(String user_name,String xm_id,String ku_id,String xm_type) {
		JSONObject result=new JSONObject();
		if ("HDB".equalsIgnoreCase(xm_type)) {
			
			
			
			
		}
		return result;
	}

	@Override
	public boolean isKyHkgn(String xmid) {
		return this.jkglDao.isKyHkgn(xmid)>0;
	}

	@Override
	public List<Map<String, Object>> listYhkAndDfInfo(JSONObject params) {
		return jkglDao.listYhkAndDfInfo(params);
	}

	@Override
	public int getYhkAndDfCount(JSONObject params) {
		return jkglDao.getYhkAndDfCount(params);
	}

	@Override
	public Map<String, Object> getYhkAndDfSum(JSONObject params) {
		return jkglDao.getYhkAndDfSum(params);
	}

	@Override
	public Map<String, Object> getJbxxInfo(String gyh) {
		return jkglDao.getJbxxInfo(gyh);
	}

	@Override
	public Map<String, Object> getYhkInfoById(String yhkid) {
		return jkglDao.getYhkInfoById(yhkid);
	}

	@Override
	@Transactional("p2p")
	public JSONObject saveDfInfo(Map<String, Object> yhkInfo,Map<String, Object> jbxxMap, String dflx, String dfgyh) {
		JSONObject result = new JSONObject();
		Map<String, Object> paramMap=new HashMap<>();
		paramMap.put("state", dflx);
		paramMap.put("yqts", null);
		paramMap.put("yqje", null);
		paramMap.put("hkDate", null);
		paramMap.put("hkId", null);
		paramMap.put("id", MapUtils.getString(yhkInfo, "ID"));
		//保存还款垫付明细
		Map<String, Object> paramMap_df=new HashMap<>();
		paramMap_df.put("REF_YHK_ID", MapUtils.getString(yhkInfo, "ID"));
		paramMap_df.put("DFR_USERNAME", dfgyh);
		paramMap_df.put("JKR_USERNAME", MapUtils.getString(yhkInfo, "REF_JKR_USERNAME"));
		paramMap_df.put("SSQS", MapUtils.getString(yhkInfo, "hkqs"));
		paramMap_df.put("YHK_DATE", MapUtils.getString(yhkInfo, "yhkDate"));
		paramMap_df.put("YHBJ", MapUtils.getString(yhkInfo, "yhbj"));
		paramMap_df.put("YHLX", MapUtils.getString(yhkInfo, "yhlx"));
		paramMap_df.put("YHK_JE", MapUtils.getString(yhkInfo, "yhhj"));
		paramMap_df.put("DF_TYPE", dflx);
		String dfId=jkglDao.insertYhkDf(paramMap_df);
		if (StringUtils.isEmpty(dfId)) {
			throw new RuntimeException("插入垫付明细失败..");
		}
		if ("2004".equals(dflx)) {
			paramMap.put("yqts", 0);
			paramMap.put("yqje", 0);
			String now=DateUtils.getCurrentTimeStr();
			paramMap.put("hkDate", now);
			//插入还款信息
			Map<String, Object> insertHk=new HashMap<>();
			insertHk.put("USER_NAME", MapUtils.getString(yhkInfo, "REF_JKR_USERNAME"));
			insertHk.put("SJHM", MapUtils.getString(yhkInfo, "SJHM"));
			insertHk.put("REF_ZQ_ID", MapUtils.getString(yhkInfo, "REF_XM_ID"));
			insertHk.put("REF_ZQ_XMMC", MapUtils.getString(yhkInfo, "REF_XM_NAME"));
			if ("01".equals(MapUtils.getString(yhkInfo, "YHK_XM_TYPE"))) {
				insertHk.put("REF_ZQ_JE", MapUtils.getString(yhkInfo, "REF_XM_JE"));
			} else {
				insertHk.put("REF_ZQ_JE", null);
			}
			insertHk.put("YHK_JE", MapUtils.getString(yhkInfo, "yhhj"));
			insertHk.put("HK_DATE", now);
			insertHk.put("HK_STATE", "01");
			insertHk.put("HK_ZL", "03");
			insertHk.put("REF_XM_ID", MapUtils.getString(yhkInfo, "REF_XM_ID"));
			insertHk.put("REF_KU_ID", MapUtils.getString(yhkInfo, "REF_KU_ID"));
			insertHk.put("REF_KU_NAME", MapUtils.getString(yhkInfo, "REF_KU_NAME"));
			if ("02".equals(MapUtils.getString(yhkInfo, "YHK_XM_TYPE"))) {
				insertHk.put("REF_KU_JE", MapUtils.getString(yhkInfo, "REF_XM_JE"));
			} else {
				insertHk.put("REF_KU_JE", null);
			}
			insertHk.put("XM_TYPE", MapUtils.getString(yhkInfo, "YHK_XM_TYPE"));
			insertHk.put("HK_QS", MapUtils.getString(yhkInfo, "hkqs"));
			insertHk.put("YHBJ", MapUtils.getString(yhkInfo, "yhbj"));
			insertHk.put("YHLX", MapUtils.getString(yhkInfo, "yhlx"));
			insertHk.put("YHQT", "0");
			insertHk.put("IS_DF", "1");
			String ref_hk_id=jkglDao.insertHk(insertHk);
			if (StringUtils.isEmpty(ref_hk_id)) {
				throw new RuntimeException("更新垫付明细表失败..");
			}
			paramMap.put("hkId", ref_hk_id);
			//更新垫付明细中真实还款信息
			Map<String, Object> updateDf=new HashMap<>();
			updateDf.put("REAL_HK_DATE", now);
			updateDf.put("REAL_YQ_TS", 0);
			updateDf.put("REAL_YQ_JE", 0);
			updateDf.put("REAL_HK_JE", MapUtils.getString(yhkInfo, "yhhj"));
			updateDf.put("ID", dfId);
			int updateYhkDf=jkglDao.updateYhkDf(updateDf);
			if (updateYhkDf<=0) {
				throw new RuntimeException("更新垫付明细表失败..");
			}
		}
		//更新应还款表
		int updateYhkInfo=jkglDao.updateYhkInfo(paramMap);
		if (updateYhkInfo<=0) {
			throw new RuntimeException("更新应还款表失败..");
		}
		double changeJe=MapUtils.getDouble(yhkInfo, "yhhj");
		double u_zhye=MapUtils.getDouble(jbxxMap, "ZHYE");
		double u_djje=MapUtils.getDouble(jbxxMap, "DJJE");
		double u_kyje=MapUtils.getDouble(jbxxMap, "KYJE");
		double l_zhye=NumberUtil.sub(u_zhye, changeJe);
		double l_djje=u_djje;
		double l_kyje=NumberUtil.sub(u_kyje, changeJe);
		//扣除垫付人账户余额
		int updateJbxx=jkglDao.updateJbxx(l_zhye, l_djje, l_kyje, dfgyh);
		if (updateJbxx<=0) {
			throw new RuntimeException("更新垫付人账户失败..");
		}
		//保存垫付人资产变动明细
		Map<String, Object> insertZc=new HashMap<>();
		insertZc.put("USERNAME", dfgyh);
		insertZc.put("ZC_CHANGE_TYPE", "6001");
		insertZc.put("ZC_CHANGE_JE", "-"+changeJe);
		insertZc.put("ZC_CHANGE_ID", dfId);
		insertZc.put("ZC_ORIGINAL_ZHYE", u_zhye);
		insertZc.put("ZC_ORIGINAL_DJJE", u_djje);
		insertZc.put("ZC_ORIGINAL_KYJE", u_kyje);
		insertZc.put("ZC_FINAL_ZHYE", l_zhye);
		insertZc.put("ZC_FINAL_DJJE", l_djje);
		insertZc.put("ZC_FINAL_KYJE", l_kyje);
		int insertZcDdtails=jkglDao.insertZcDdtails(insertZc);
		if (insertZcDdtails<=0) {
			throw new RuntimeException("插入垫付人账户变动明细失败..");
		}
		result.put("res", "1");
		result.put("msg", "垫付成功!");
		return result;
	}
	

}
